package com.lehoon.tomato.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.lehoon.tomato.global.UserContext;
import com.lehoon.tomato.model.Bedroom;
import com.lehoon.tomato.model.RegisterParam;
import com.lehoon.tomato.model.User;
import com.lehoon.tomato.model.common.Result;
import com.lehoon.tomato.model.common.ValidateException;
import com.lehoon.tomato.service.SchoolService;
import com.lehoon.tomato.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 *
 * @author wang
 * @since 2020-09-23
 */
@RestController
@RequestMapping("/auth/user/")
@Api(description = "用户相关")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private SchoolService schoolService;

    /**
     * 注册用户
     * @return
     */
    @PostMapping("register")
    @ApiOperation(value = "普通用户注册")
    public Result registerUser(RegisterParam registerParam){
        User user = new User();
        BeanUtils.copyProperties(registerParam,user);
        Integer userId = UserContext.getUser().getId();
        user.setId(userId);
        userService.updateUserInfo(user);
        return new Result();
    }

    @PostMapping("chooseSchoolAddress")
    @ApiOperation(value = "用户选择学校地址")
    public Result chooseSchoolAddress(Integer areaId,Integer cityId,Integer provinceId,Integer regionId,Integer schoolId,Integer bedroomId){
        if (cityId==null||provinceId==null||regionId==null||schoolId==null||bedroomId==null){
           throw new ValidateException("参数不能为空");
        }
        //TODO 校验学校是否正确
       // Bedroom bedroom =
        User user = new User();
        Integer userId = UserContext.getUser().getId();
        user.setId(userId);
        user.setSchoolId(schoolId);
        user.setRegionId(regionId);
        user.setProvinceId(provinceId);
        user.setBedroomId(bedroomId);
        user.setAreaId(areaId);
        userService.updateUserAddress(user);
        return new Result();
    }

}

